package stack;

import java.util.Stack;

public class QuestionTwo {
    //用两栈实现队列

    class MyQueue {
        Stack<Integer> leftStack ;
        Stack<Integer> rightStack;
        public MyQueue() {
            leftStack = new Stack<>();
            rightStack = new Stack<>();
        }

        public void push(int x) {
            leftStack.push(x);
        }

        public int pop() {
            //先把lwft中所有数据 放到rightStack中 然后取出right中最顶上那一个 在重新吧数据放到left
            if (leftStack.empty()) return -1;
            change(leftStack,rightStack);
            int result = rightStack.pop();
           change(rightStack,leftStack);
            return result;
        }

        public int peek() {
            if (leftStack.empty()) return -1;
            change(leftStack,rightStack);
            int result = rightStack.peek();
            change(rightStack,leftStack);
            return result;
        }

        public boolean empty() {
            return leftStack.empty();
        }
        //方法复用
        public void change(Stack<Integer> one,Stack<Integer> two){
            while(!one.empty()){
                two.push(one.pop());
            }
        }
    }
}
